package org.dromara.tearoom.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.tearoom.domain.bo.TerAccountsBo;
import org.springframework.stereotype.Service;
import org.dromara.tearoom.domain.bo.TerTransactionsBo;
import org.dromara.tearoom.domain.vo.TerTransactionsVo;
import org.dromara.tearoom.domain.TerTransactions;
import org.dromara.tearoom.mapper.TerTransactionsMapper;
import org.dromara.tearoom.service.ITerTransactionsService;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 钱包充值交易流水Service业务层处理
 *
 * @author xch
 * @date 2025-05-08
 */
@RequiredArgsConstructor
@Service
public class TerTransactionsServiceImpl implements ITerTransactionsService {

    private final TerTransactionsMapper baseMapper;

    /**
     * 查询钱包充值交易流水
     *
     * @param id 主键
     * @return 钱包充值交易流水
     */
    @Override
    public TerTransactionsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询钱包充值交易流水列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 钱包充值交易流水分页列表
     */
    @Override
    public TableDataInfo<TerTransactionsVo> queryPageList(TerTransactionsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerTransactions> lqw = buildQueryWrapper(bo);
        Page<TerTransactionsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的钱包充值交易流水列表
     *
     * @param bo 查询条件
     * @return 钱包充值交易流水列表
     */
    @Override
    public List<TerTransactionsVo> queryList(TerTransactionsBo bo) {
        LambdaQueryWrapper<TerTransactions> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerTransactions> buildQueryWrapper(TerTransactionsBo bo) {
        LambdaQueryWrapper<TerTransactions> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(TerTransactions::getCreateTime);
        lqw.ge(bo.getStartTime()!= null,TerTransactions::getCreateTime, bo.getStartTime());
        lqw.le(bo.getEndTime()!= null,TerTransactions::getCreateTime, bo.getEndTime());
        lqw.eq(bo.getMemberId() != null, TerTransactions::getMemberId, bo.getMemberId());
        lqw.eq(bo.getAmount() != null, TerTransactions::getAmount, bo.getAmount());
        lqw.eq(bo.getBalanceBefore() != null, TerTransactions::getBalanceBefore, bo.getBalanceBefore());
        lqw.eq(bo.getBalanceAfter() != null, TerTransactions::getBalanceAfter, bo.getBalanceAfter());
        lqw.eq(bo.getAccountId() != null, TerTransactions::getAccountId, bo.getAccountId());
        return lqw;
    }

    /**
     * 新增钱包充值交易流水
     *
     * @param bo 钱包充值交易流水
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerTransactionsBo bo) {
        TerTransactions add = MapstructUtils.convert(bo, TerTransactions.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改钱包充值交易流水
     *
     * @param bo 钱包充值交易流水
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerTransactionsBo bo) {
        TerTransactions update = MapstructUtils.convert(bo, TerTransactions.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }
    @Override
    public int queryBalance(String start, String end){
        return baseMapper.selectTotalAmount(start,end);
    }
    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerTransactions entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除钱包充值交易流水信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
